package fzrj.qkl.demo.controller;

import fzrj.qkl.demo.model.CommonResponse;
import fzrj.qkl.demo.service.backend.UserBackendService;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserBackendService userBackendService;

    // 注册
    @PostMapping(value = "/add")
    public CommonResponse addUser(@RequestParam String username, @RequestParam String passwd) {

        boolean ok = userBackendService.registerUser(username, passwd);
        if (ok) {
            return CommonResponse.ok(ok);
        }

        return CommonResponse.fail("4001",new Exception("error"));
    }


    // 验证
    @PostMapping("/check")
    public CommonResponse checkUser(@RequestParam int id, @RequestParam String passwd) throws ContractException {

        boolean ok = userBackendService.checkUser(id, passwd);
        if (ok) {
            return CommonResponse.ok(ok);
        }

        return CommonResponse.fail("4001",new Exception("error"));
    }

}